package com.tjhq.commons.dict.web;

import com.tjhq.commons.dict.external.po.DictTFactorPO;
import com.tjhq.commons.dict.external.po.DictTModelPO;
import com.tjhq.commons.dict.external.service.IDictTModelService;
import com.tjhq.commons.dict.service.IDictTFactorSelfService;
import com.tjhq.commons.dict.service.IDictTModelSelfService;
import com.tjhq.commons.dict.util.DictDBConstants;
import com.tjhq.commons.inputcomponent.po.ResultPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pc on 2017/11/3.
 */
@RestController
@RequestMapping("/dict")
public class DictTestController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IDictTModelSelfService dictTModelSelfService;
    @Resource
    private IDictTModelService dictTModelService;
    @Resource
    private IDictTFactorSelfService dictTFactorSelfService;
    
    @RequestMapping(value = "/create" , produces = "application/json; charset=utf-8")
    public Object creatTable(HttpServletRequest request){
        DictTModelPO dtm = new DictTModelPO();
        dtm.setDealtype("A0");
        dtm.setDbtablename("A_TEST");
        dtm.setName("测试表");
        dtm.setAppid("BIL");
        dtm.setIspartition("1");
        dtm.setSuitid("321524791FAC2273E05400237D3D3C30");
        // 物理表
        dtm.setTabletype(DictDBConstants.TABLE_TYPE_TABLE);
        try {
            dictTModelSelfService.insertDictTModelForPhysics(dtm);
        } catch (Exception e) {
            logger.error(e.getMessage(),e.fillInStackTrace());
            return new ResultPO("CODE",e.getMessage());
        }
        return new ResultPO("SUCCESS");
    }
    
    @RequestMapping(value = "/insert" , produces = "application/json; charset=utf-8")
    public Object insertFactor() {
        Map str = new HashMap();
        DictTFactorPO dtf = new DictTFactorPO();
        dtf.setTableid("c544a912b0f84a8389e446193b00f03a");
        dtf.setName("测试");
        dtf.setDbcolumnname("CS");
        dtf.setDatatype(3);
        dtf.setDatalength(200);
        DictTModelPO dtm = dictTModelService.getDictTModelByID(dtf.getTableid(), false);
        String tabletype = dtm.getTabletype();
        try {
            // 表类型：1物理表、2（不更新）视图表、3（可更新）视图表
            dictTFactorSelfService.initNewFactor(dtf, dtm);
            if ("1".equals(tabletype)) {// 1物理表
                dictTFactorSelfService.insertDictTFactorForPhysics(dtf, dtm, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            str.put("error", e.getMessage().replace("\"", "\'"));
            return str;
        }
        if (str.get("error") == null) {
            str.put("success", "保存成功");
        }
        return str;
    }
    
}
